import React, { useState } from 'react'

import expainExcel from './expainExcel'
import { exceDataCheck } from './detailExcelCheck'

export default function index() {

    const [fileList, setFileList] = useState([])
    const [importList, setImportList] = useState([])
    const [loading, setLoading] = useState(false)
    const uploadProps = {
        name: 'file',
        maxCount: 1,
        accept: '.xls,.xlsx,application/vnd.ms-excel',
        fileList: fileList,
        headers: {
            authorization: 'authorization-text',
        },

        onChange(info) {
            if (info.file.status !== 'uploading') {
                setLoading(true)
            } else {
                setLoading(false)
            }
            setFileList(info.fileList)
        },
        onRemove: (file) => {
            setFileList([])
            setImportList([])
        },
        beforeUpload: (file) => {
            return new Promise((resolve, reject) => {
                let fileNameList = file.name.split('.')
                if (!['xlsx', 'xls'].includes(fileNameList[fileNameList.length - 1])) {
                    message.error('文件类型不正确或解析出错')
                    reject(false)
                }
                setFileList([])
                expainExcel(file).then(r => {
                    setLoading(false)
                    let result = exceDataCheck(r)
                    if (!result.checkFlg) {
                        MessageChannel.error(result.message)
                        return
                    }
                    setImportList(result.arr)
                    resolve(true)
                }, r => {
                    message.error('文件类型不正确或解析出错')
                    reject(false)
                })
            })
        }
    };

    return (
        <div>
            <Upload {...uploadProps}>
                <Button>上传</Button>
            </Upload>
        </div>
    )
}



